<?php

	echo $form->create("Language",array('id'=>"language_translate",'class'=>'language_translate',"url"=>"/".$this->params['url']['url']));
	$i = 0;
	$template = $form->input('Language.translation.[[i]].msgid',array("class"=>"translations","type"=>"text","style"=>"display:none","div"=>false,"label"=>false,'value'=>"[[tranlateStringKey]]"));
	$template .= "<div class='string'>";
	$template .= "<label>[[tranlateStringKey]]</label>";
	$template .= $form->input('Language.translation.[[i]].msgstr',array('class'=>"translations","type"=>"text",'value'=>"[[stringValue]]",'label'=>false));
	$template .= "</div>";
	foreach($tranlateStrings as $tranlateStringKey => $string)
	{
		if(strlen($tranlateStringKey) == 0)
		{
			continue;
		}
		echo Utility::replaceTemplateVars($template,array("i"=>$i,"tranlateStringKey"=>$tranlateStringKey,"stringValue"=>$string));
		$i++;
	}
	echo $form->end(__("Save"));
	$javascript->link("/cms/js/json.js");
	echo $form->create("Language",array("id"=>"language_translate_dummy","url"=>"/".$this->params['url']['url']));
	echo $form->input("json",array("id"=>"translate_json")); 
	echo "</form>";
?>
<script>
	$("#language_translate input[type='submit']").click(function(e){
		e.preventDefault();
		populateDummyForm();
		$("#language_translate_dummy")[0].submit();
		return false;
	});
	function populateDummyForm()
	{
		var jsonObject = {};
		var translationElements = $('#language_translate').find("input.translations");
		translationElements.each(function(){
			var name = $(this).attr("name");
			var keyIndex = name.replace("data[Language][translation][","").match(/^\d+/);
			if(keyIndex.length != 1)
			{
				return;
			}
			var keyIndex = keyIndex[0];
			if(typeof jsonObject[keyIndex] == "undefined")
			{
				jsonObject[keyIndex] = {};
			}
			if(name == "data[Language][translation]["+keyIndex+"][msgstr]")
			{
				jsonObject[keyIndex]["msgstr"] = $(this).val();
			}else if(name == "data[Language][translation]["+keyIndex+"][msgid]")
			{
				jsonObject[keyIndex]["msgid"] = $(this).val();
			}
		});
		$("#translate_json").val(JSON.stringify(jsonObject));
	}
</script>